/**
 *
 * @author ilya portnyagin iportnyagin@gmail.com
 */

package ru.portnyagin.helpdeskru.service;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import ru.portnyagin.helpdeskru.model.ServiceObject;
import ru.portnyagin.helpdeskru.model.ServiceObject_;
import ru.portnyagin.helpdeskru.model.UserHD;

@Stateless
public class ServiceObjectService extends AbstractService<ServiceObject> {
    @PersistenceContext(unitName = "HelpDeskRuPU")
    private EntityManager em;

    
    protected EntityManager getEntityManager() {
        return em;
    }

    public ServiceObjectService() {
        super(ServiceObject.class);
    }
    
    public List<ServiceObject> findServiceObject(UserHD curUser) {
        CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
        CriteriaQuery cq = cb.createQuery();
        Root<ServiceObject> s = cq.from(ServiceObject.class);
        
        cq.where(cb.equal(s.get(ServiceObject_.organization), curUser.getOrganization()));
        
        cq.orderBy(cb.asc(s.get(ServiceObject_.id)));
        cq.select(s);
        
        return getEntityManager().createQuery(cq).getResultList();

        
    }
}
